package functions.ems_accountant_job_pool;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.beans.AccountingPoolGroupUserBean;
import com.beans.CategoryBean;
import com.beans.CompanyBean;
import com.beans.GroupBasicBean;
import com.beans.ORGBean;
import com.crc.mam.BaseCase;
import coms.Constants;
import coms.JSONToBean;
import frame.HttpHelper;

/**
 * 会计任务池
 * @author CHENHAILIAN6
 *
 */
public class AccountGroupWork extends BaseCase {
	private static Logger logger = LoggerFactory.getLogger(AccountGroupWork.class);
	
	
	ArrayList<AccountingPoolGroupUserBean> accountUsers;
	
	public ArrayList<AccountingPoolGroupUserBean> getAccountUsers() {
		return accountUsers;
	}
	public void setAccountUsers(ArrayList<AccountingPoolGroupUserBean> accountUsers) {
		this.accountUsers = accountUsers;
	}
	
	
	/**
	 * 只查询有效的工作组
	 * @param i  0表示初审审核工作范围组 , 1 表示复审审核小组
	 * useFlay 0 表示有效
	 * findAccountGroupWorkListUrl="ms-accountant-job-pool/accountGroupWork/findAccountGroupWorkList"
	 */
	public void findAccountGroupWorkList(int i) {
		logger.info("只查询有效的工作组 ----- start -------" );
		String params = "{\"page\":{\"currentPage\":1,\"pageSize\":10,\"recordCount\":31},\"params\":{\"groupType\":\""+i+"\",\"groupName\":\"\",\"useFlay\":\"0\",\"ouCode\":\"\",\"ouName\":\"\",\"deptName\":\"\",\"deptId\":\"\",\"expCategoryName\":\"\",\"expCategoryCode\":\"\"},\"sortDTO\":{\"fieldName\":\"\",\"orderBy\":\"\"}}";
		String url = Constants.FIND_ACCOUNT_GROUP_WORK_LIST_URL;
		try {
			result = HttpHelper.post(params, url);
			status = HttpHelper.isOk;
			if(status) {
			logger.info("findAccountGroupWorkList--------" + result);
			JSONObject object = new JSONObject(result);
			int total = object.getInt("total");
			/**
			 * 如果需要选择不同组，此处加判断
			 */
			if(total > 0) {
			  	
			}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.info("只查询有效的工作组 ----- end -------" );
	}
	
	/**
	 * 通过groupId查询组员信息
	 * @param groupId
	 * findCrewsByGroupIdUrl="/ems-accountant-job-pool/group/findCrewsByGroupId?groupId="
	 */
	public void findCrewsByGroupId(String groupId) {
		logger.info("通过groupId查询组员信息 ----- start -------" );
		String params = null;
		String url = Constants.FIND_CREWS_BY_GROUPID_URL+groupId;
		try {
			result = HttpHelper.post(params, url);
			status = HttpHelper.isOk;
			logger.info("findCrewsByGroupId--------" + result);
			if(status) {
			  try {
			  JSONArray array = new JSONArray(result);
			  int total = array.length();
			  if(total > 0) {
				accountUsers = new ArrayList<AccountingPoolGroupUserBean>();
				for(int t=0;t<array.length();t++) {
					JSONObject group = array.getJSONObject(t);
					AccountingPoolGroupUserBean bean =	JSONToBean.parseJson2Object(AccountingPoolGroupUserBean.class, group.toString());
					accountUsers.add(bean);
				}							
			}
			}catch(JSONException e) {
			  e.printStackTrace();
			  status = false;
			  remark = "JSON解析错误";
			}
			}else {
			  try {
			    JSONObject object = new JSONObject(result);
			    remark = object.getString("message");
			    logger.error(remark);
			  }catch(JSONException e) {
			    e.printStackTrace();
			    remark = "JSON解析错误";
			  }
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			remark = "系统异常";
			status = false;
		}
		logger.info("通过groupId查询组员信息 ----- end -------" );
	}
	
	
	/**
	 * 通过组id和人名得到人员信息
	 * @param groupId
	 * @param userName
	 * @return
	 */
	public AccountingPoolGroupUserBean get(String groupId) {
		AccountingPoolGroupUserBean bean = null;
		findCrewsByGroupId(groupId);
		if(accountUsers != null) {	
		  int total = accountUsers.size();
		  if(total >0) {
		    status = true;
		    bean = accountUsers.get(0);
		    remark = bean.toString();
		  }else {
		    status = false;
		  }
		}else {
		  status = false;
		}
		return bean;
	}
	

	/**
	 * 创建小组工作范围
	 * @param groupType 0,1 核算，复核
	 * @param groupBasicBean
	 * @param useFlay 0,1 启用，禁用
	 * @param companyBean
	 * @param categoryBean
	 * @param deptBean
	 * @param rateValue 优先级
	 */
	public void save(String groupType,GroupBasicBean groupBasicBean,String useFlay,CompanyBean companyBean,CategoryBean categoryBean,ORGBean deptBean,String rateValue ) {
	  	  
	  String params = "{\"groupType\":\""+groupType+"\",\"groupName\":\""+groupBasicBean.getGroupName()+"\","
	      + "\"groupId\":"+groupBasicBean.getGroupId()+",\"useFlay\":\""+useFlay+"\",\"ouCode\":\""+companyBean.getCompanycode()+"\","
	          + "\"ouName\":\""+companyBean.getCompanyName()+"\",\"expCategoryName\":\""+categoryBean.getCategoryName()+"\","
	              + "\"expCategoryCode\":\""+categoryBean.getCategoryCode()+"\",\"rateValue\":\"\",\"modifyRateValue\":\""+rateValue+"\","
	                  + "\"deptName\":\""+deptBean.getTitle()+"\",\"deptId\":\""+deptBean.getId()+"\",\"workspaceId\":\"\"}";
	   logger.info(" save group work param --- " + params);
	  String url = Constants.SAVE_URL;
	    try {
	      result = HttpHelper.post(params, url);
	      status = HttpHelper.isOk;
	      logger.info("save group--------" + result);
	      if(status) {
	        try {
	          JSONObject object = new JSONObject(result);
	          String msg = object.getString("message");
	          String stateCode = object.getString("stateCode");
	          if(stateCode.equals("200")) {
	            status = true;
	           }else {
	             status = false;
	             remark = msg;
	             logger.error("添加小组工作范围-- " + msg);
	           }
	        }catch(JSONException e) {
	          remark = "JSON解析错误";
	        }
	      }else {
	        try {
	          JSONObject object = new JSONObject(result);
	          remark = object.getString("message");
	          logger.error("添加小组工作范围-- " + remark);
	        }catch(JSONException e) {
	          e.printStackTrace();
	          remark = "JSON解析错误";
	        }
	      }
	    }catch(Exception e) {
	      status = false;
	      remark = "系统异常";
	      e.printStackTrace();
	    }
	}
	
}
